var SERVER_HOST = "localhost:8000";

function addParam(url, name, value) {
    if (url.indexOf("?") > 0) {
        return url + "&" + name + "=" + value;
    } else {
        return url + "?" + name + "=" + value;
    }
}

function loadMainContent() {
    var url = window.location.href;
    url = addParam(url, 'ajax', 1);

    $.get(url, function(data) {
        $('#content').html(data);
        addEvents();
    });
}

function addTask(project_id) {
    var url = "http://" + SERVER_HOST + "/p/" + project_id + "/add-or-update?"
        + "name=" + encodeURIComponent($("#name").val())
        + "&due_date=" + encodeURIComponent($("#due_date").val())
        + "&start_date=" + encodeURIComponent($("#start_date").val())
        + "&estimated_mds=" + encodeURIComponent($("#estimated_mds").val())
        + "&assignee=" + encodeURIComponent($("#assignee").val())
        + "&tag=" + encodeURIComponent($("#tag").val())
        + "&status=" + encodeURIComponent($("#status").val())
        + "&note=" + encodeURIComponent($("#note").val())
        + "&id=" + encodeURIComponent($("#task_id").val())

    $('#add_or_update_task_dialog').dialog('close');
    $.get(url, function(data) {
        loadMainContent();
    });
}

function completeTask(project_id, id) {
    var url = "http://" + SERVER_HOST + "/p/" + project_id + "/complete?id=" + id
    
    $('#confirm_text').html("确定完成了 <b>" + $('#name_' + id).html() + "</b> 吗?"); 
    $( "#confirm_dialog" ).dialog({
        resizable: false,
        height:200,
        modal: true,
        show: "slide",
        hide: "explode",
        title: '完成',
        buttons: {
            "确定": function() {
                var theDialog = this;
                $.get(url, function(data) {
                    $(theDialog).dialog( "close" );
                    updateStatusBar(data);
                });

            },
            "取消": function() {
                $(this).dialog( "close" );
            }
        }
    });

}

function editTask(id) {
    $('#task_id').val(id);
    $('#name').val($.trim($('#name_' + id).text()));
    $('#due_date').val($.trim($('#due_date_' + id).text()));
    $('#group_due_date').val($.trim($('#group_due_date_' + id).text()));
    $('#tag').val($.trim($('#tag_' + id).text()));
    $('#note').val($.trim($('#note_' + id).text()));
    $('#estimated_mds').val($.trim($('#estimated_mds_' + id).text()));
    $('#start_date').val($.trim($('#start_date_' + id).text()));
    $('#end_date').val($.trim($('#end_date_' + id).text()));

    var assignee = $('#assignee_' + id).html()
    $('#' + assignee).attr('selected', 'selected');
    $('#ss_' + $('#status_id_' + id).html()).attr('selected', 'selected');

    $('#add_or_update_task_dialog').dialog({
        modal: true,
        height: 'auto',
        show: 'slide',
        width: 'auto', 
        title: '编辑任务'
    });
}

function addNewTask(id) {
    /*
    $('#task_id').val('');
    $('#name').val('');
    $('#due_date').val('');
    $('#tag').val('');
    $('#note').val('');
    $('#estimated_mds').val('');
    $('#start_date').val('');
    $('#end_date').val('');
    */

    $('#task_id').val($.trim($('#task_id').text()));
    $('#name').val($.trim($('#name').text()));
    $('#due_date').val($.trim($('#due_date').text()));
    $('#tag').val($.trim($('#tag').text()));
    $('#note').val($.trim($('#note').text()));
    $('#estimated_mds').val($.trim($('#estimated_mds').text()));
    $('#start_date').val($.trim($('#start_date').text()));
    $('#end_date').val($.trim($('#end_date').text()));

    var assignee = $('#assignee_' + id).html()
    $('#' + assignee).attr('selected', 'selected');
    $('#ss_' + $('#status_id_' + id).html()).attr('selected', 'selected');

    $('#add_or_update_task_dialog').dialog({
        modal: true,
        show: "explode",
        hide: "explode",
        title: '新建任务',
        height: 'auto',
        width: 'auto' 
    });
}
function deleteTask(projec_id, id) {
    var url = "http://" + SERVER_HOST + "/p/" + project_id + "/delete?id=" + id

    $('#confirm_text').html("确定要删除 <b>" + $('#name_' + id).html() + "</b> 吗?"); 
    $( "#confirm_dialog" ).dialog({
        modal: true,
        show: "explode",
        hide: "explode",
        resizable: false,
        title: '删除',
        height:200,
        buttons: {
            "确定": function() {
                var theDialog = this;
                $.get(url, function(data) {
                    $('#confirm_dialog').dialog('close');
                    updateStatusBar(data);
                });

            },
            "取消": function() {
                $(this).dialog( "close" );
            }
        }
    });
}

function showCheckDates() {
    $('.due_date').show();    
    $('.group_due_date').show();    
}

function hideCheckDates() {
    $('.due_date').hide();    
    $('.group_due_date').hide();    
}

function addEvents() {
    var dayNamesMin = ['日', '一', '二', '三', '四', '五', '六'];
    var datePickerConfig = {dayNamesMin: dayNamesMin, dateFormat: 'yy-mm-dd'};
    $('#due_date').datepicker(datePickerConfig);
    $('#start_date').datepicker(datePickerConfig);
    $('#end_date').datepicker(datePickerConfig);
    $('.button').button();
}

function updateStatusBar(data) {
    var msg = "操作失败";
    var color = "red";

    if (data == "OK") {
        msg = "操作成功";
        color = "green";
    }

    $('#status_bar').show()
                    .css('background-color', color)
                    .css('width', '500px')
                    .html(msg);
    $('#status_bar').delay(500).fadeOut('slow', function() {
    });
    loadMainContent();
}

$(function() {
    $.ajaxSetup({cache: false});
    hideCheckDates();    
     
    var theme = $.cookie('jquery_ui_theme');
    if (theme == null) {
        theme = 'flick';
    }

    loadMainContent();
    $('head').prepend('<link id="theme_css" type="text/css" href="/static/css/jquery-ui/' + theme + '/jquery-ui-1.8.5.custom.css" rel="stylesheet" />');

    $("#jquery_ui_theme").change(function(index, value) {
        var theme = $('#jquery_ui_theme').val();
        $('#theme_css').attr(
            'href',
            '/static/css/jquery-ui/' + theme + '/jquery-ui-1.8.5.custom.css'
        );

        $.cookie('jquery_ui_theme', theme, {expires: 1000});
    });
});

